package com.yxhl.zoume.core.func.pay.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.View;

import com.yxhl.zoume.R;
import com.yxhl.zoume.common.ui.activity.base.BaseActivityWithToolBar;
import com.yxhl.zoume.core.func.pay.fragment.CarPayFragment;
import com.yxhl.zoume.core.func.pay.fragment.ExpressCarBillDetailFragment;
import com.yxhl.zoume.core.func.pay.model.event.ExpressCarBillDetailEvent;
import com.yxhl.zoume.core.specialcar.model.CarPaymentInfo;
import com.yxhl.zoume.di.component.base.HasComponent;
import com.yxhl.zoume.di.component.car.DaggerSpecialCarPayComponent;
import com.yxhl.zoume.di.component.car.SpecialCarPayComponent;
import com.yxhl.zoume.di.module.ActivityModule;

import rx.functions.Action1;

/**
 * Created by vhawk on 2016/10/11.
 */
public class CarPaymentActivity extends BaseActivityWithToolBar implements HasComponent<SpecialCarPayComponent> {

    private static final String KEY_PAYMENT_INFO = "PAYMENT_INFO";
    private SpecialCarPayComponent component;
    private CarPaymentInfo paymentInfo;

    public static Intent getCallingIntent(Activity activity, CarPaymentInfo paymentInfo) {
        Intent intent = new Intent(activity, CarPaymentActivity.class);
        intent.putExtra(KEY_PAYMENT_INFO, paymentInfo);
        return intent;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        hideLeftIcon();
        addDefaultFragment();
        onRxBusEvent();
    }

    private void onRxBusEvent() {
        getCompositeSubscription().add(toSubscription(ExpressCarBillDetailEvent.class, new Action1<ExpressCarBillDetailEvent>() {
            @Override
            public void call(ExpressCarBillDetailEvent expressCarBillDetailEvent) {
                replaceFragment(getSupportFragmentManager().findFragmentByTag("CarPayFragment"),
                        ExpressCarBillDetailFragment.newInstance(expressCarBillDetailEvent, paymentInfo.getCarPaymentEntrance()),
                        R.id.special_car_pay_container,
                        true, "ExpressCarBillDetailFragment");
            }
        }));
    }

    private void addDefaultFragment() {
        paymentInfo = (CarPaymentInfo) getIntent().getSerializableExtra(KEY_PAYMENT_INFO);
        addFragment(R.id.special_car_pay_container, CarPayFragment.newInstance(paymentInfo), "CarPayFragment");
    }

    @Override
    protected void onHeaderLeftClick(View view) {
        onBackPressed();
    }

    @Override
    protected void onHeaderRightClick(View view) {
        onBackPressed();
    }

    @Override
    public void onBackPressed() {
        if (canBack()) {
            super.onBackPressed();
            setToolbarTitle("订单支付");
        }
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_special_car_pay;
    }

    @Override
    protected void initializeInjector() {
        component = DaggerSpecialCarPayComponent.builder()
                .appComponent(getAppComponent())
                .activityModule(new ActivityModule(this))
                .build();
    }

    @Override
    public SpecialCarPayComponent getComponent() {
        return component;
    }

    private boolean canBack() {
        Fragment expressCarBillDetailFragment = getSupportFragmentManager().findFragmentByTag("ExpressCarBillDetailFragment");
        return expressCarBillDetailFragment != null && expressCarBillDetailFragment.isVisible();
    }
}
